package com.yogee.topseller.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.bean.MyOrderListBean;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称：TopSeller
 * 类描述：
 * 创建时间：2017/7/12 0012 11:07
 * 创建人：wangye
 * 修改备注：
 */

public class ReservationAdapter extends RecyclerView.Adapter<ReservationAdapter.MyViewHolder> {

    private Context context;
    private int type;
    private List<MyOrderListBean.ProductListBean> beans;
    private OnStatusClickListener onStatusClickListener;

    public ReservationAdapter(Context context, int type) {
        this.context = context;
        this.type = type;
    }

    public void setOnStatusClickListener(OnStatusClickListener onStatusClickListener) {
        this.onStatusClickListener = onStatusClickListener;
    }

    public void addData(List<MyOrderListBean.ProductListBean> beans) {
        this.beans = beans;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_reservation, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        switch (type) {
            case 0:
                holder.saveTv.setText("取消");
                holder.saveTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onStatusClickListener.onCancleClick(beans.get(position).getOrderId());
                    }
                });
                break;
            case 1:
                if (beans.get(position).getIsUpload().equals("0")) {
                    holder.saveTv.setText("上传凭证");
                    holder.saveTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onStatusClickListener.onSaveClick(beans.get(position).getOrderId());
                        }
                    });
                } else {
                    holder.saveTv.setVisibility(View.GONE);
                }
                break;
            case 2:
                if (beans.get(position).getIsReturnBack().equals("0")) {
                    holder.saveTv.setText("回寄合同");
                    holder.saveTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            onStatusClickListener.onReturnContract(beans.get(position).getOrderId());
                        }
                    });
                } else {
                    holder.saveTv.setVisibility(View.GONE);
                }
                break;
            case 3:
                holder.saveTv.setVisibility(View.GONE);
                break;
        }

        switch (beans.get(position).getStatus()) {
            case "1":
                holder.statusTv.setText("待确认");
                break;
            case "2":
                holder.statusTv.setText("已确认");
                break;
            case "3":
                holder.statusTv.setText("打款到账");
                break;
            case "4":
                holder.statusTv.setText("确认回寄合同");
                break;
            case "5":
                holder.statusTv.setText("已结佣");
                break;

        }

        holder.detailsRl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onStatusClickListener.onDetailsClick(beans.get(position).getOrderId());
            }
        });


        holder.title.setText(beans.get(position).getName());
        holder.syTv.setText(beans.get(position).getExpectReturn());
        holder.yjTv.setText(beans.get(position).getCommission());
        holder.qxTv.setText(beans.get(position).getInvestmentTerm());
        holder.tv_type.setText(beans.get(position).getProType());

        // 预热和图片
        if(beans.get(position).getImgstatus().equals("1")){
            holder.img_tu.setVisibility(View.VISIBLE);
        }else{
            holder.img_tu.setVisibility(View.GONE);
        }

        if(beans.get(position).getStatus().equals("预热中"))
            holder.img_yu.setVisibility(View.VISIBLE);
        else{
            holder.img_yu.setVisibility(View.GONE);
        }


    }

    @Override
    public int getItemCount() {
        return beans == null ? 0 : beans.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_reservation_save_tv)
        TextView saveTv;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.item_reservation_sy_tv)
        TextView syTv;
        @BindView(R.id.item_reservation_qx_tv)
        TextView qxTv;
        @BindView(R.id.item_reservation_yj_tv)
        TextView yjTv;
        @BindView(R.id.item_reservation_status_tv)
        TextView statusTv;
        @BindView(R.id.item_order_details_rl)
        RelativeLayout detailsRl;
        @BindView(R.id.tv_type)
        TextView tv_type;
        @BindView(R.id.img_yu)
        ImageView img_yu;
        @BindView(R.id.img_tu)
        ImageView img_tu;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnStatusClickListener {
        void onCancleClick(String orderId);

        void onSaveClick(String orderId);

        void onReturnContract(String orderId);

        void onDetailsClick(String orderId);
    }
}
